# coding = utf-8

import flask
import os
from threading import Thread
from Chat import chat
from pylogger import pylogger as log
__version__ = "2.1.7"

app = flask.Flask(__name__)
def runWebServer():
    os.system('python ./Chat/chat.py')### NOTE:: sys.executable


@app.errorhandler(404)
def page_not_found(error):
    return flask.render_template('PageNotFound.html'), 404


@app.route('/')
def redirect():
    return flask.redirect('/chat'), 301


@app.route('/robots.txt')
def robots():
    return flask.render_template('robots.txt')


@app.route('/PageNotFound')
def error():
    return flask.render_template('PageNotFound.html'), 301

# address = input('webServer_address:\n')
address = "127.0.0.1:5000"  ## DEBUG
with open('./static/sources/webserver.js') as f:
    fr = f.readlines()
    fr[1] = '"'+address+'";\n'
with open('./static/sources/webserver.js', 'w') as f:
    f.write(''.join(fr))
app.register_blueprint(chat,url_prefix="/chat")
Thread(target=runWebServer).start()
log.init()
app.run('127.0.0.1', 5701)